order={};
exp_inds={};
for tnd=1:size(proj_meta(cur_site).raw_data,2)
    nbr_frames=proj_meta(cur_site).raw_data(1,tnd).nbr_frames;
    if length(nbr_frames)>5
        for xnd=1:length(nbr_frames)
            if nbr_frames(xnd)==5000
                if xnd==length(nbr_frames)
                    order{tnd}(xnd)=3;
                else
                    order{tnd}(xnd)=1;
                end
            else
                order{tnd}(xnd)=2;
            end
        end
        ord_ind=1:length(order{tnd});
        order{tnd}(max(ord_ind(order{tnd}==2))+1:end)=3;
    else
        order{tnd}=[1 2 1 2 3];
    end
    exp_inds{tnd}(1,1)=1;
    exp_inds{tnd}(1,2)=nbr_frames(1);
    for fnd=2:length(order{tnd})
        exp_inds{tnd}(fnd,1)=exp_inds{tnd}(fnd-1,2)+1;
        exp_inds{tnd}(fnd,2)=sum(nbr_frames(1:fnd));
    end
end

shad_all={};
vis_fl_all={};
delta_all={};
for tnd=1:size(proj_meta(cur_site).raw_data,2)
    stim_id=proj_meta(cur_site).raw_data(1,tnd).stim_id;
    runM=proj_meta(cur_site).raw_data(1,tnd).velM_ind;
    runP=proj_meta(cur_site).raw_data(1,tnd).velP_ind;
    pupil_pos=proj_meta(cur_site).raw_data(1,tnd).pupil_pos;
    shad=[];
    vis_fl=[];
    cnt=1;
    for bnd=1:length(order{tnd})
        if order{tnd}(bnd)==1
            tmp_runM=runM(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            tmp_stim_id=stim_id(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            for fnd=1:length(tmp_runM)
                if tmp_runM(fnd)==0
                    shad(cnt)=0;
                    vis_fl(cnt)=0;
                    cnt=cnt+1;
                elseif tmp_runM(fnd)==1 & logical(tmp_stim_id(fnd))==0 %feedback
                    shad(cnt)=1;
                    vis_fl(cnt)=1;
                    cnt=cnt+1;
                elseif tmp_runM(fnd)==1 & logical(tmp_stim_id(fnd))==1 %feedback mismatch
                    if tmp_stim_id(fnd)==7
                        shad(cnt)=3;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    else
                        shad(cnt)=4;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    end
                end
            end
        elseif order{tnd}(bnd)==2
            tmp_runM=runM(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            tmp_stim_id=stim_id(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            tmp_runP=runP(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            for fnd=1:length(tmp_runM)
                if logical(tmp_stim_id(fnd))==0 & tmp_runM(fnd)==0 & tmp_runP(fnd)==0 | logical(tmp_stim_id(fnd))==1 & tmp_runM(fnd)==0 & tmp_runP(fnd)==0
                    shad(cnt)=0;
                    vis_fl(cnt)=0;
                    cnt=cnt+1;
                elseif logical(tmp_stim_id(fnd))==1 & tmp_runM(fnd)==0 & tmp_runP(fnd)==1
                    if tmp_stim_id(fnd)==7
                        shad(cnt)=5;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    else
                        shad(cnt)=6;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    end
                elseif logical(tmp_stim_id(fnd))==0 & tmp_runM(fnd)==0 & tmp_runP(fnd)==1
                    shad(cnt)=2;
                    vis_fl(cnt)=1;
                    cnt=cnt+1;
                elseif logical(tmp_stim_id(fnd))==0 & tmp_runM(fnd)==1 & tmp_runP(fnd)==1
                    shad(cnt)=1;
                    vis_fl(cnt)=1;
                    cnt=cnt+1;
                elseif logical(tmp_stim_id(fnd))==0 & tmp_runM(fnd)==1 & tmp_runP(fnd)==0 | logical(tmp_stim_id(fnd))==1 && tmp_runM(fnd)==1 & tmp_runP(fnd)==0
                    shad(cnt)=3;
                    vis_fl(cnt)=0;
                    cnt=cnt+1;
                elseif logical(tmp_stim_id(fnd))==1 & tmp_runM(fnd)==1 & tmp_runP(fnd)==1
                    if tmp_stim_id(fnd)==7
                        shad(cnt)=3;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    else
                        shad(cnt)=4;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    end
                end
            end
        end
    end
    shad_all{tnd}=shad;
    vis_fl(length(vis_fl):length(runM))=0;
    vis_fl_all{tnd}=vis_fl;
    delta=[];
    for ind=1:size(pupil_pos,2)-1
        dx=abs(pupil_pos(1,ind)-pupil_pos(1,ind+1));
        dy=abs(pupil_pos(2,ind)-pupil_pos(2,ind+1));
        delta(ind)=sqrt(dx^2+dy^2);
        if ind==size(pupil_pos,2)-1
            delta(ind+1)=delta(ind);
        end
    end
    delta_all{tnd}=delta;
end

all_act=[];
for tnd=1:size(proj_meta(cur_site).raw_data,2)
    cnt=1;
    runM=proj_meta(cur_site).raw_data(1,tnd).velM_ind;
    base_frame=exp_inds{tnd}(find(order{tnd}==3,1,'first')-1,2);
    runM(1:base_frame)=[];
    for ind=1:proj_meta(cur_site).nbr_piezo_layers
        for pnd=1:size(proj_meta(cur_site).raw_data(ind,tnd).smoothed_ROIs,2)
            cur_act=proj_meta(cur_site).raw_data(ind,tnd).smoothed_ROIs(pnd).activity;
%             bas_act=cur_act;
%             bas_act(1:base_frame)=[];
%             baseline=bas_act(runM==0);
%             baseline=median(baseline);
%             all_act{tnd}(cnt,:)=cur_act/baseline;
            all_act{tnd}(cnt,:)=cur_act/median(cur_act);
            cnt=cnt+1;
        end
    end
end

pb=[];
fb=[];
rdk=[];
bdk=[];

for tnd=1:size(proj_meta(cur_site).raw_data,2)
    pb(tnd)=mean(mean(all_act{tnd}(:,shad_all{tnd}==2)));
    fb(tnd)=mean(mean(all_act{tnd}(:,shad_all{tnd}==1)));
    
    runM=proj_meta(cur_site).raw_data(1,tnd).velM_ind;
    runM(1:exp_inds{tnd}(find(order{tnd}==3)-1,2))=0;
    rdk(tnd)=mean(mean(all_act{tnd}(:,runM==1)));
    
    runM=proj_meta(cur_site).raw_data(1,tnd).velM_ind;
    runM(1:exp_inds{tnd}(find(order{tnd}==3)-1,2))=1;
    bdk(tnd)=mean(mean(all_act{tnd}(:,runM==0)));
end

fb=fb-1;
pb=pb-1;
rdk=rdk-1;
bdk=bdk-1;

figure;
plot(fb,'color',[0.5 0.5 0.5],'linewidth',2)
hold on
plot(pb,'g','linewidth',2)
plot(rdk,'b','linewidth',2)
plot(bdk,'k','linewidth',2)
plot(sum([pb;rdk]),'r')

ffm=[];
for tnd=1:size(proj_meta(cur_site).raw_data,2)
    cur_ffm=shad_all{tnd}==3;
    init=exp_inds{tnd}(find(order{tnd}==2),1);
    fin=exp_inds{tnd}(find(order{tnd}==2),2);
    for gnd=1:length(init)
        cur_ffm(init(gnd):fin(gnd))=0;
    end
    ffm(tnd)=mean(mean(all_act{tnd}(:,cur_ffm)));
end
ffm=ffm-1;
figure;plot(ffm)

runPerct=get_runPerctMeta(proj_meta,cur_site);